package com.power.mediators
{
	import com.power.enum.ConfigUrl;
	import com.power.enum.NotificationTypes;
	import com.power.events.HouseRoomEvent;
	import com.power.events.RoomCategorChangeEvent;
	import com.power.interfaces.IViewMediator;
	import com.power.model.Notification;
	import com.power.model.RoomVo;
	import com.power.model.factories.RoomFactory;
	import com.power.views.RoomWindowView;
	import com.power.views.components.RoomButton;
	import com.power.views.components.RoomTag;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	public class RoomWindowViewMediator implements IViewMediator
	{
		private var _roomView:RoomWindowView;
		
		private var _roomCategroys:Array;
		
		private var _roomVos:Vector.<RoomVo>;
		
		private var _houserMediator:HouseWindowViewMediator;
		
		private var roomFactory:RoomFactory;
		
		public function RoomWindowViewMediator()
		{
			
		}
		
		public function initView(view:DisplayObject):void
		{
			_roomView = view as RoomWindowView;
			setAllEventListener();
		}
		
		public function initRoomView(view:DisplayObject,houserMediator:HouseWindowViewMediator):void
		{
			_roomView = view as RoomWindowView;
			_houserMediator = houserMediator;
			setAllEventListener();
		}
		
		private function setAllEventListener():void
		{
			_roomView.addEventListener(RoomCategorChangeEvent.ROOM_CATEGROY_CHANGE,onAppEventHandler);
			_roomView.addEventListener(HouseRoomEvent.ROOM_LIST_EVENT,changeHouseHandler);
		}
		public function onAppEventHandler(e:RoomCategorChangeEvent):void
		{
			var notification:Notification = e.notification;
			handleNotification(notification);
		}
		public function changeHouseHandler(e:HouseRoomEvent):void
		{	
			_houserMediator.handleNotification(e.notification);
		}
		
		public function dispatchEvent(type:String, data:Object):void
		{
		}
		
		public function handleNotification(notification:Notification):void
		{
			var nofi_type:String = notification.type;
			switch(nofi_type)
			{
				case NotificationTypes.CHANGE_ROOM_CATEGRORYS: changeRoomCateGorys(notification);
			}
		}
		private function changeRoomCateGorys(notification:Notification):void
		{
			
			trace("changing room categorys!");
			var roomtype:String = notification.body.roomtype;
			trace(roomtype);
			//update the roomtype list
			roomFactory = new RoomFactory();
			roomFactory.startLoadRoomAssets(ConfigUrl.ROOM_URL,roomtype);
			roomFactory.addEventListener("ParserRoomDataEndByType",changeRoomPicListHandler);
			
			
		}
		public function initCategroys(cates:Array):void
		{
			_roomCategroys = cates;
			for each(var label:String in _roomCategroys)
			{
				var roomBtn:RoomButton = new RoomButton();
				roomBtn.setButtonLabel(label);
				_roomView.roomCategoryButton.addElement(roomBtn);
			}
		}
		public function initRoomTags(roomvos:Vector.<RoomVo>):void
		{
			_roomView.roomTags.removeAllElements();
			for each(var vo:RoomVo in roomvos)
			{
				var roomTag:RoomTag = new RoomTag();
				roomTag.setRoomVo(vo);
				_roomView.roomTags.addElement(roomTag);
				
			}
		}
		
		public function changeRoomPicListHandler(e:Event):void
		{
			initRoomTags(roomFactory.getRoomData())
		}
		
		public function hide():void
		{
		}
		
		public function show():void
		{
		}
	}
}